package com.example.test.controller;

import com.example.test.annotation.TestBlankConstraint;
import com.example.test.config.ConfigC;
import com.example.test.vo.ResultVO;
import com.example.test.entity.Product;
import com.example.test.service.MyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


//@RestController Spring框架4版本之后出来的注解,之前版本返回json数据需要@ResponseBody配合@Controller
//@RequestMapping 配置url映射关系
//@PathVariable 获取url中的数据
//@RequestParam 获取请求参数的值
//@GetMapping 组合注解
@RestController
public class MyController {
    private static final Logger logger = LoggerFactory.getLogger(MyController.class);

    @Autowired
    private MyService myService;

    @Autowired
    private ConfigC configC;

    @Autowired
    private RestTemplate restTemplate;

    private List myStrList = Collections.synchronizedList(new ArrayList());


    //https://baijiahao.baidu.com/s?id=1738225440175143238&wfr=spider&for=pc
    @GetMapping("/hello")
    @Retryable(value = RestClientException.class, maxAttempts = 3,
            backoff = @Backoff(delay = 5000L, multiplier = 2))
    @TestBlankConstraint
    public void hello() {
        myService.say();
        configC.hello();
        //RemoteAccessException、ResourceAccessException
    }

    @GetMapping("/hello2")
    //https://www.cnblogs.com/minimeta/p/13722512.html
    public ResultVO hello2() throws URISyntaxException {
        URI uri = new URI("https://7bad36e2-c450-41a8-91b0-63cc255212d3.mock.pstmn.io/api/avgsalary");
        Product[] array = restTemplate.getForObject(uri, Product[].class);
        List<Product> list = Arrays.asList(array);
        return new ResultVO<>(list);
    }

}
